package com.action;

import com.pojo.Demo;
import com.pojo.DemoVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Controller
@RequestMapping("/")
public class TextController {

    @GetMapping("j/index")
    public String jIndex(Model model) {
        System.out.println("jsp index");
        model.addAttribute("msg", "hello jsp:" + LocalDateTime.now());
        return "jsp/index";
    }

    @GetMapping("j/index5")
    public String jIndex5(Model model) {
        System.out.println("jsp index");
        model.addAttribute("msg", "hello jsp:" + LocalDateTime.now());
        return "jsp/index5";
    }

    @GetMapping("j/list")
    public String jList(Model model) {
        System.out.println("jsp index");
        model.addAttribute("list", getList("jsp"));
        return "jsp/index";
    }

    @GetMapping("j/dialog")
    public String jDialog(Model model,
                          @RequestParam(value="callBackName", defaultValue = "dialogConfirm") String callBackName) {
        model.addAttribute("callBackName", callBackName);
        return "jsp/dialog";
    }

    @GetMapping("j/dialog2")
    public String jDialog2(Model model,
                          @RequestParam(value="id", defaultValue = "id") String id,
                           @RequestParam(value="name", defaultValue = "name") String name) {
        model.addAttribute("id", id);
        model.addAttribute("name", name);
        return "jsp/dialog2";
    }

    @GetMapping("j/page")
    public String page(Model model, @RequestParam(value="pageNum", defaultValue = "1") Integer pageNum,
                         @RequestParam(value="pageSize", defaultValue = "10") Integer pageSize) {
        System.out.println("page index");
        model.addAttribute("list", getPageList(pageNum, pageSize));
        return "jsp/page";
    }

    // 分页demo
    @GetMapping("j/index2")
    public String index2(Model model, @RequestParam(value="pageNum", defaultValue = "1") Integer pageNum,
                         @RequestParam(value="current", defaultValue = "1") Integer current,
                         @RequestParam(value="pageSize", defaultValue = "10") Integer pageSize) {
        System.out.println("page index");
        model.addAttribute("list", getPageList(pageNum, pageSize));
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("total",30);
        model.addAttribute("current",current);
        return "jsp/index2";
    }

    @GetMapping("j/index3")
    public String index3(Model model, @RequestParam(value="pageNum", defaultValue = "1") Integer pageNum,
                         @RequestParam(value="current", defaultValue = "1") Integer current,
                         @RequestParam(value="pageSize", defaultValue = "10") Integer pageSize) {
        System.out.println("page index");
        model.addAttribute("list", getPageList(pageNum, pageSize));
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("total",30);
        model.addAttribute("current",current);
        return "jsp/index3";
    }

    @GetMapping("t/index")
    public String tIndex(Model model) {
        System.out.println("jsp index");
        model.addAttribute("msg", "hello jsp:" + LocalDateTime.now());
        return "thymeleaf/index";
    }

    @GetMapping("t/list")
    public String tList(Model model) {
        System.out.println("jsp index");
        model.addAttribute("list", getList("jsp"));
        return "thymeleaf/index";
    }

    @GetMapping("f/index")
    public String fIndex(Model model) {
        System.out.println("jsp index");
        model.addAttribute("msg", "hello jsp:" + LocalDateTime.now());
        return "freemarker/index";
    }

    @GetMapping("f/list")
    public String fList(Model model) {
        System.out.println("jsp index");
        model.addAttribute("list", getList("jsp"));
        return "freemarker/index";
    }
    private List<Demo> getList(String name) {
        return IntStream.range(1, 20).mapToObj(i -> new Demo(i+"",
                name + "_name_" + i)).collect(Collectors.toList());
    }

    private List<Demo> getPageList(Integer pageNum, Integer pageSize) {
        return IntStream.range(1, pageSize+1).mapToObj(i -> new Demo(""+(i+(pageNum-1)*pageSize),
                "name")).collect(Collectors.toList());
    }

    @PostMapping("save/demo")
    @ResponseBody
    public Map<String, Object> saveDemo(@RequestBody Demo d) {
        System.out.println(d);
        return new HashMap<String, Object>(){{put("success", true);}};
    }

    @PostMapping("save/demo/ids")
    @ResponseBody
    public Map<String, Object> saveDemoIds(@RequestBody List<String> ids) {
        System.out.println(ids);
        return new HashMap<String, Object>(){{put("success", true);}};
    }

    @PostMapping("save/demos")
    @ResponseBody
    public Map<String, Object> saveDemos(@RequestBody List<Demo> demos) {
        System.out.println(demos);
        return new HashMap<String, Object>(){{put("success", true);}};
    }

    @PostMapping("save/demoVo")
    @ResponseBody
    public Map<String, Object> saveDemoVo(@RequestBody DemoVo demo) {
        System.out.println(demo);
        return new HashMap<String, Object>(){{put("success", true);}};
    }
}
